﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;

namespace EasyMail
{
    /// <summary>
    /// An email message object can be constructed for use by an SmtpClient or child class.
    /// Not too different from a System.Net.Mail.MailMessage.
    /// </summary>
    public class EasyMailMessage : MailMessage
    {
        public String subject {get; set;}
        public String body { get; set; }

        /// <summary>
        /// Constructor for a Message that takes only Strings as parameters.
        /// </summary>
        /// <param name="recipientAddress">The email address of the recipient</param>
        /// <param name="fromAddress">The email address of the sender</param>
        /// <param name="subject">The subject of the email message</param>
        /// <param name="body">The body of the email message</param>
        public EasyMailMessage(String recipientAddress, String fromAddress, String subject, String body)
        {
            To.Add(new MailAddress(recipientAddress));
            From = new MailAddress(fromAddress);
            this.subject = subject;
            this.body = body;
        }
        
        /// <summary>
        /// Constructor for a Message that uses a combination of MailAddress objects and Strings.
        /// This is superior to the String-only method in that a MailAddress object can contain a name of the sender/recipient so that it looks more professional.
        /// </summary>
        /// <param name="recipientAddress">A MailAddress object containing data for the recipient</param>
        /// <param name="fromAddress">A MailAddress object containing data for the sender</param>
        /// <param name="subject">The subject of the email message</param>
        /// <param name="body">The body of the email message</param>
        public EasyMailMessage(MailAddress recipientAddress, MailAddress fromAddress, String subject, String body)
        {
            To.Add(recipientAddress);
            From = fromAddress;
            this.subject = subject;
            this.body = body;
        }

        /// <summary>
        /// A contructor with no parameters.
        /// </summary>
        public EasyMailMessage() { }

        /// <summary>
        /// Adds another recipient to the email
        /// </summary>
        /// <param name="recipientAddress">A MailAddress object of a recipient</param>
        public void AddRecipient(String recipientAddress)
        {
            To.Add(new MailAddress(recipientAddress));
        }

        /// <summary>
        /// Adds another recipient to the email
        /// </summary>
        /// <param name="recipientAddress">A String of a recipient email address</param>
        public void AddRecipient(MailAddress recipientAddress)
        {
            To.Add(recipientAddress);
        }

        /*
        /// <summary>
        /// Represents the object with a string
        /// </summary>
        /// <returns>A string representation of the email message object</returns>
        public override String ToString()
        {
            String aboutString = "From:[" + From.Address + "], Subject:[" + subject + "], Body:[" + body + "]";
            return aboutString;
        }
        */
    }

    /// <summary>
    /// A static class for sending the email using Outlook.com
    /// </summary>
    public static class OutlookComSender
    {
        public static void Send(MailMessage message, String password)
        {
            SmtpClient sender = new SmtpClient("smtp.live.com", 587);
            sender.EnableSsl = true;
            sender.Credentials = new System.Net.NetworkCredential(message.From.Address, password);
            try
            {
			    sender.Send(message);
            }
			catch (Exception ex)
            {
			  Console.WriteLine("Exception caught in CreateTestMessage1(): {0}", ex.ToString() );
            }              
		}
    }

    /// <summary>
    /// A static class for sending the email using Gmail
    /// </summary>
    public static class GmailSender
    {
        public static void Send(MailMessage message, String password)
        {
            SmtpClient sender = new SmtpClient("smtp.gmail.com", 465);
            sender.EnableSsl = true;
            sender.Credentials = new System.Net.NetworkCredential(message.From.Address, password);
            try
            {
                sender.Send(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught in CreateTestMessage1(): {0}", ex.ToString());
            }
        }
    }

    /// <summary>
    /// A static class for sending the email using Yahoo
    /// </summary>
    public static class YahooSender
    {
        public static void Send(MailMessage message, String password)
        {
            SmtpClient sender = new SmtpClient("smtp.mail.yahoo.com", 465);
            sender.EnableSsl = true;
            sender.Credentials = new System.Net.NetworkCredential(message.From.Address, password);
            try
            {
                sender.Send(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught in CreateTestMessage1(): {0}", ex.ToString());
            }
        }
    }

    /// <summary>
    /// A static class for sending the email using iCloud
    /// </summary>
    public static class iCloudSender
    {
        public static void Send(MailMessage message, String password)
        {
            SmtpClient sender = new SmtpClient("smtp.mail.me.com", 587);
            sender.EnableSsl = true;
            sender.Credentials = new System.Net.NetworkCredential(message.From.Address, password);
            try
            {
                sender.Send(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught in CreateTestMessage1(): {0}", ex.ToString());
            }
        }
    }
}
